home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Beta / Quicktime 2.0 Beta.iso / Programming Stuff / Sample Code / DTS Sample Code / MyComponent ƒ / MyComponent.c next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  4.9 KB  |  207 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        MyComponent.c
  3.     
  4.     Contains:    simple component sample.
  5.  
  6.     Written by:    John Wang
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <1>        03/14/94    JW        Re-Created for Universal Headers.
  13.  
  14.     To Do:
  15.     
  16. */
  17.  
  18. #ifdef THINK_C
  19. #define        applec
  20. #endif
  21.  
  22. #include    <Memory.h>
  23. #include    <Errors.h>
  24. #include    <Components.h>
  25.  
  26. #include    "MyComponent.h"
  27. #include    "MyComponentRoutines.h"
  28.  
  29. /* ------------------------------------------------------------------------- */
  30.  
  31. //    Component entry point.
  32.  
  33. pascal ComponentResult main(ComponentParameters *params, char **storage)
  34. {
  35.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  36.     long                ret;
  37.  
  38.     if ( kDEBUGME )
  39.         DebugStr("\pIn main()");
  40.     
  41.     if ( params->what < 0 ) { 
  42.         switch ( params->what ) {
  43.             case kComponentOpenSelect:
  44.                 return ( CallComponentFunction(params, (ComponentFunctionUPP) MyOpen) );
  45.     
  46.             case kComponentCloseSelect:
  47.                 return ( CallComponentFunctionWithStorage(storage, params,
  48.                         (ComponentFunctionUPP) MyClose) );
  49.                 
  50.             case kComponentCanDoSelect:
  51.                 ret = CallComponentFunction(params, (ComponentFunctionUPP) MyCanDo);
  52.                 if ( ret == false ) {
  53.                     DebugStr("\pIn kComponentCanDoSelect");
  54.                     if ( (**myPrivateGlobals).delegate != 0 ) {
  55.                         ret = DelegateComponentCall(params, (**myPrivateGlobals).delegate);
  56.                     }
  57.                 }
  58.                 return ( ret );
  59.     
  60.             case kComponentVersionSelect: 
  61.                 return ( CallComponentFunction(params, (ComponentFunctionUPP) MyVersion) );
  62.     
  63.             case kComponentRegisterSelect: 
  64.                 return ( CallComponentFunction(params, (ComponentFunctionUPP) MyRegister) );
  65.  
  66.             case kComponentTargetSelect: 
  67.                 return ( CallComponentFunctionWithStorage(storage, params,
  68.                         (ComponentFunctionUPP) MyTarget) );
  69.  
  70.             default:
  71.                 return ( paramErr );
  72.         }
  73.     } else {
  74.         switch ( params->what ) {
  75.             case kMyProcedureSelect:    
  76.                 return ( CallComponentFunctionWithStorage(storage, params,
  77.                         (ComponentFunctionUPP) MyProcedure) );
  78.                             
  79.             default:
  80.                 return( paramErr );
  81.         }
  82.     }
  83. }
  84.  
  85. /* ------------------------------------------------------------------------- */
  86.  
  87. //    Required component calls.
  88.  
  89. pascal ComponentResult MyOpen(ComponentInstance self)
  90. {
  91.     SharedGlobals         *mySharedGlobals;
  92.     PrivateGlobals         **myPrivateGlobals;
  93.     
  94.     if ( kDEBUGME )
  95.         DebugStr("\pIn MyOpen()");
  96.         
  97.     mySharedGlobals = nil;
  98.     myPrivateGlobals = nil;
  99.     
  100.     //    Create private variables.
  101.     myPrivateGlobals = (PrivateGlobals **) NewHandleClear(sizeof(PrivateGlobals));
  102.     if ( myPrivateGlobals == nil )
  103.         goto bail;
  104.     
  105.     //    Create shared variables if refcon == nil.
  106.     mySharedGlobals=(SharedGlobals *)GetComponentRefcon((Component) self);
  107.     if ( mySharedGlobals == nil ) {
  108.         mySharedGlobals=(SharedGlobals *) NewPtrSysClear(sizeof(SharedGlobals));
  109.         if ( mySharedGlobals == nil )
  110.             goto bail;
  111.         SetComponentRefcon((Component) self, (long) mySharedGlobals);
  112.  
  113.         //    Initialize shared variables.
  114.         mySharedGlobals->shared = 0;
  115.     }
  116.  
  117.     //    Initialize private variables.
  118.     (**myPrivateGlobals).delegate = 0;
  119.     (**myPrivateGlobals).self = self;
  120.     
  121.     //    Since we've gotten here, everyt hings ok and we can set up the connection.
  122.     SetComponentInstanceStorage(self, (Handle) myPrivateGlobals);
  123.     return ( noErr );
  124.  
  125. bail:
  126.     if ( myPrivateGlobals )
  127.         DisposeHandle((Handle) myPrivateGlobals);
  128.     if ( mySharedGlobals )
  129.         DisposePtr((Ptr) mySharedGlobals);
  130.     return ( memFullErr );
  131. }
  132.  
  133. pascal ComponentResult MyClose(Handle storage, ComponentInstance self)
  134. {
  135.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  136.     SharedGlobals         *mySharedGlobals;
  137.  
  138.     if ( kDEBUGME )
  139.         DebugStr("\pIn MyClose()");
  140.  
  141.     //    Dispose of private variables.
  142.     if ( myPrivateGlobals )
  143.         DisposeHandle((Handle) myPrivateGlobals);
  144.     
  145.     //    Dispose of globals variables if last instance.
  146.     if ( CountComponentInstances((Component) self) == 1 ) {
  147.         mySharedGlobals = (SharedGlobals *) GetComponentRefcon((Component) self);
  148.         if ( mySharedGlobals != nil )
  149.             DisposePtr((Ptr) mySharedGlobals);
  150.     }
  151.     
  152.     return ( noErr );
  153. }
  154.  
  155. pascal ComponentResult MyCanDo(short selector)
  156. {    
  157.     if ( kDEBUGME )
  158.         DebugStr("\pIn MyCanDo()");
  159.     
  160.     switch ( selector ) {
  161.     
  162.         //    Required component calls.
  163.         case kComponentOpenSelect:
  164.         case kComponentCloseSelect:
  165.         case kComponentCanDoSelect:
  166.         case kComponentVersionSelect: 
  167.         case kComponentRegisterSelect: 
  168.         case kComponentTargetSelect: 
  169.  
  170.         //    MyComponent specific calls.
  171.         case kMyProcedureSelect:    
  172.             return ( true );
  173.  
  174.         //    Not handled.
  175.         default:
  176.             return ( false );
  177.     }
  178. }
  179.  
  180. pascal ComponentResult MyVersion()
  181. {
  182.     if ( kDEBUGME )
  183.         DebugStr("\pIn MyVersion()");
  184.  
  185.     return ( (kMyComponentSpec<<16) | (kMyComponentVersion) );
  186. }
  187.  
  188. pascal ComponentResult MyRegister()
  189. {
  190.     if ( kDEBUGME )
  191.         DebugStr("\pIn MyRegister()");
  192.  
  193.     return ( false );
  194. }
  195.  
  196. pascal ComponentResult MyTarget(Handle storage, ComponentInstance self)
  197. {
  198.     PrivateGlobals         **myPrivateGlobals = (PrivateGlobals **) storage;
  199.  
  200.     if ( kDEBUGME )
  201.         DebugStr("\pIn MyTarget()");
  202.  
  203.     //    From now on, self will be the component instance that targeted us.
  204.     (**myPrivateGlobals).self = self;
  205.     
  206.     return ( noErr );
  207. }